package com.qf.sy2103.ssm.controller;

import com.qf.sy2103.ssm.constants.Constants;
import com.qf.sy2103.ssm.entity.Kehu;
import com.qf.sy2103.ssm.entity.User;
import com.qf.sy2103.ssm.service.KehuService;
import com.qf.sy2103.ssm.vo.KehuVo;
import com.qf.sy2103.ssm.vo.MessageVo;
import com.qf.sy2103.ssm.vo.PageVo;
import com.qf.sy2103.ssm.vo.TableVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Objects;

@Controller
@RequestMapping("/kehu")
public class KehuController {
    @Autowired
    private KehuService kehuService;
    @RequestMapping("/kehuAll")
    @ResponseBody
    public TableVo<Kehu> kehuAll(PageVo pageVo,String name,Integer state){
        Kehu kehu=new Kehu();
        kehu.setNickname(name);
        kehu.setState(state);
        TableVo<Kehu> all = kehuService.all(pageVo, kehu);

        return all;
    }
    @RequestMapping("/add")
    @ResponseBody
    public MessageVo  kehuAdd(KehuVo kehuVo){
        Kehu kehu=new Kehu(kehuVo.getUsername(),kehuVo.getPassword(),kehuVo.getNickname(),kehuVo.getAddress(),kehuVo.getMoney(),kehuVo.getState());
        int add = kehuService.add(kehu);
        if (add==1){
            MessageVo messageVo = new MessageVo(Constants.USER_CODE_ADDSUCCESS, Constants.USER_MSG_ADDSUCCESS, null);
            return messageVo;
        }
        MessageVo messageVo = new MessageVo(Constants.USER_CODE_ADDFAIL, Constants.USER_MSG_ADDFAIL, null);
        return messageVo;
    }
    @RequestMapping("/update")
    @ResponseBody
    public MessageVo kehuEdit(KehuVo kehuVo){
        Kehu kehu=new Kehu(kehuVo.getUsername(),kehuVo.getPassword(),kehuVo.getNickname(),kehuVo.getAddress(),kehuVo.getMoney(),kehuVo.getState());
        kehu.setId(kehuVo.getId());
        int edit = kehuService.updata(kehu);

            if (edit==1){
                MessageVo messageVo = new MessageVo(Constants.USER_CODE_EDITSUCCESS, Constants.USER_MSG_EDITSUCCESS, null);
                return messageVo;
            }
        MessageVo messageVo = new MessageVo(Constants.USER_CODE_EDITFAIL, Constants.USER_MSG_EDITFAIL, null);
        return messageVo;
    }
    @RequestMapping("/del")
    @ResponseBody
    public MessageVo kehuDel(Integer[] ids){
        int del = kehuService.del(ids);
        if (del>0){
            MessageVo messageVo = new MessageVo(Constants.USER_CODE_DELETESUCCESS, Constants.USER_MSG_DELETESUCCESS, null);
            return messageVo;
        }
        MessageVo messageVo = new MessageVo(Constants.USER_CODE_DELETEFAIL, Constants.USER_MSG_DELETEFAIL, null);
        return messageVo;
    }
}
